package com.example.mvc.config;

import com.example.mvc.interceptors.TokenInterceptor;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenInterceptor())
                .addPathPatterns("/**");
    }

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webserverCustomizer(ServerProperties serverProperties) {
        return factory -> {
            System.err.println(" ---> application.yaml server.port = "
                    + serverProperties.getPort());
            factory.setPort(9999);
        };
    }
}
